(function ($) {
  "use strict";
  // 下拉刷新
  var PTR = function(el, opt) {
    if (typeof opt === typeof function () {}) {
      opt = {
        onRefresh: opt
      }
    }
    if (typeof opt === typeof 'a') {
      opt = undefined
    }
    this.opt = $.extend(PTR.defaults, opt || {});
    this.container = $(el);
    this.attachEvents();
  }
  PTR.defaults = {
    distance: 50, // 范围 Number
    onRefresh: undefined, // 刷新 function
    onPull: undefined // 下拉 function
  }
  PTR.prototype.touchStart = function(e) {
    if(this.container.hasClass("refreshing")) return;
    var p = $.getTouchPosition(e);
    this.start = p;
    this.diffX = this.diffY = 0;
  };
  PTR.prototype.touchMove= function(e) {
    if(this.container.hasClass("refreshing")) return;// 刷新中
    if(!this.start) return false;// 初始位置
    if(this.container.scrollTop() > 0) return;// 在顶部
    var p = $.getTouchPosition(e);// x,y
    this.diffX = p.x - this.start.x;// 偏移X
    this.diffY = p.y - this.start.y;// 偏移Y
    if (Math.abs(this.diffX) > Math.abs(this.diffY)) return true; // 说明是左右方向的拖动
    if(this.diffY < 0) return;// 向上
    this.container.addClass("touching");// 拖动中
    e.preventDefault();
    e.stopPropagation();
    this.diffY = Math.pow(this.diffY, 0.75);
    this.container.css("transform", "translate3d(0, "+this.diffY+"px, 0)");
    this.pulling(this.diffY)
  };
  PTR.prototype.touchEnd = function() {
    this.start = false;
    if(this.diffY <= 0 || this.container.hasClass("refreshing")) return;
    this.container.removeClass("touching");
    this.container.css("transform", "");
    if(Math.abs(this.diffY) <= this.opt.distance) {
    } else {
      this.refreshing();
    }
  };
  PTR.prototype.refreshing = function() {// 刷新操作
    this.pulling(this.opt.distance)
    this.container.addClass("refreshing");
    if (this.opt.onRefresh) {
      this.opt.onRefresh.call(this)
    }
    this.container.trigger("refresh");
  }
  PTR.prototype.pulling = function(diffY) {// 下拉操作
    if(diffY < this.opt.distance) {
      // 范围内
    } else {
      // 范围外
    }
    if (this.opt.onPull) {
      this.opt.onPull.call(this, Math.floor(diffY / this.opt.distance * 100))
    }
    this.container.trigger("pull");
  }
  PTR.prototype.pullToRefreshDone = function() {
    this.container.removeClass("refreshing");
  }
  PTR.prototype.attachEvents = function() {
    var el = this.container;
    $('<div class="gzl-pullrefresh__loading"><img src="../../assets/img/common/loading.gif"><span>快放手，<br>小易要去旅行了啦~</span></div>').prependTo(el);
    el.addClass("gzl-pullrefresh");
    el.on($.touchEvents.start, $.proxy(this.touchStart, this));
    el.on($.touchEvents.move, $.proxy(this.touchMove, this));
    el.on($.touchEvents.end, $.proxy(this.touchEnd, this));
  };
  var pullToRefreshDone = function(el) {
    $(el).removeClass("refreshing");
  }
  $.fn.pullToRefresh = function(opt) {
    return this.each(function() {
      var $this = $(this)
      var ptr = $this.data('ptr')
      if (!ptr) $this.data('ptr', ptr = new PTR(this, opt))
      if (typeof opt === typeof 'a') {
        ptr[opt].call(ptr)
      }
    });
  }
  $.fn.pullToRefreshDone = function() {
    return this.each(function() {
      pullToRefreshDone(this);
    });
  }
})($);